﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RssWindowsPhone.ServiceReference;
using System.Windows.Navigation;

namespace RssWindowsPhone
{
    public partial class CoreApp : PhoneApplicationPage
    {
        String user;
        String password;

        public CoreApp()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.FeedAddErrorBox.Text = "";
            NavigationContext.QueryString.TryGetValue("User", out this.user);
            NavigationContext.QueryString.TryGetValue("Password", out this.password);
            ServiceRssClient client = new ServiceRssClient();
            LoginModel log = new LoginModel()
            {
                Password = this.password,
                UserName = this.user,
                RememberMe = false
            };
            client.GetFluxAsync(log);
            client.GetFluxCompleted += new EventHandler<GetFluxCompletedEventArgs>(client_GetFluxCompleted);
        }


        void client_GetFluxCompleted(object sender, GetFluxCompletedEventArgs e)
        {
           List<Flux> listFlux = e.Result.ToList();
           HomeFeedList.ItemsSource = listFlux;
            if(this.pvtController.SelectedItem != this.News)
                this.pvtController.SelectedItem = this.News;
            if (this.FeedAddErrorBox.Text.Length > 0)
                this.FeedAddErrorBox.Text = "";
          
        }

        private void FeedAddBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.FeedAddErrorBox.Text.Length > 0)
                this.FeedAddErrorBox.Text = "";

            ServiceRssClient client = new ServiceRssClient();
            LoginModel log = new LoginModel()
            {
                Password = this.password,
                UserName = this.user,
                RememberMe = false
            };

            RegisterRss newRss = new RegisterRss()
            {
                Link = this.FeedAddUrlTxtBox.Text,
                Title = this.FeedAddTitleTxtBox.Text,
                UserName = this.user
            };

            client.AddRssAsync(log, newRss);
            client.AddRssCompleted += new EventHandler<AddRssCompletedEventArgs>(client_AddRssCompleted);
        }

        void client_AddRssCompleted(object sender, AddRssCompletedEventArgs e)
        {
            FluxStatus status = e.Result;

            if (status != FluxStatus.Success)
                this.FeedAddErrorBox.Text = status.ToString();
            else
            {
                ServiceRssClient client = new ServiceRssClient();
                LoginModel log = new LoginModel()
                {
                    Password = this.password,
                    UserName = this.user,
                    RememberMe = false
                };
                client.GetFluxAsync(log);
                client.GetFluxCompleted += new EventHandler<GetFluxCompletedEventArgs>(client_GetFluxCompleted);
            }

        }

        private void selectedFeed_Click(object sender, RoutedEventArgs e)
        {
            Button _button = (Button)sender;
            Object obj = _button.DataContext;
            Flux flux = obj as Flux;
            ServiceRssClient client = new ServiceRssClient();
            this.NavigationService.Navigate(new Uri("/RssReader.xaml?User="+this.user+"&Password="+this.password+"&Id="+flux.ID.ToString()+"&Link="+flux.Link+"&Title="+flux.Title, UriKind.Relative));
        }

        private void editFeed_Click(object sender, RoutedEventArgs e)
        {
            Button _button = (Button)sender;
            Object obj = _button.DataContext;
            Flux flux = obj as Flux;

            this.NavigationService.Navigate(new Uri("/ModifyRss.xaml?User=" + this.user + "&Password=" + this.password + "&FeedID=" + flux.ID.ToString() + "&FeedLink=" + flux.Link + "&FeedTitle=" + flux.Title, UriKind.Relative));
        }

        private void removeFeed_Click(object sender, RoutedEventArgs e)
        {
            Button _button = (Button)sender;
            Object obj = _button.DataContext;
            Flux flux = obj as Flux;
            ServiceRssClient client = new ServiceRssClient();

            DeleteRss toDelete = new DeleteRss()
            {
                IdFlux = flux.ID,
                UserName = this.user
            };

            LoginModel log = new LoginModel()
            {
                Password = this.password,
                UserName = this.user,
                RememberMe = false
            };

            client.RemoveRssAsync(log, toDelete);
            client.RemoveRssCompleted += new EventHandler<RemoveRssCompletedEventArgs>(client_RemoveRssCompleted);
            
            return;
        }

        void client_RemoveRssCompleted(object sender, RemoveRssCompletedEventArgs e)
        {
            ServiceRssClient client = new ServiceRssClient();
            LoginModel log = new LoginModel()
            {
                Password = this.password,
                UserName = this.user,
                RememberMe = false
            };
            client.GetFluxAsync(log);
            client.GetFluxCompleted += new EventHandler<GetFluxCompletedEventArgs>(client_GetFluxCompleted);
        }

    }
}